import {defineStore} from "pinia";
import {Reactive, reactive, Ref, ref} from "vue";
import {get, post} from "@/request";
import {ElMessage} from "element-plus";

interface current_row {
    base_name: string;
    base_deptname: string;
    sericnum: string;
    name: string;
    nameen: string;
    pid: string;
    fuzename: string;
    city: string;
    address: string;
    tel: string;
    fax: string;
    sort: string;
    id: string;
    edit: string;
    sort_num: string;
}

interface res {
    data: current_row[],
    page: number,
    total: number
}

export const useUnitStore = defineStore("Unit", () => {
    const units: Reactive<current_row[]> = reactive<current_row[]>(
        [] as current_row[]
    );
    const current_row: Reactive<current_row> = reactive<current_row>(
        {} as current_row
    );
    const page = ref(1)
    const total = ref(1)
    const search_val = ref("");
    const search_obj = ref([
        {label: "名称", key: "name", value: ''},
        {label: "地址", key: "address", value: ''},
    ]);
    const search_obj_task=ref([
        {label: "名称", value: ""},
        {label: "编号", value: ""},
        {label: "项目类型", value: ""},
        {label: "开始时间", value: ""},
        {label: "负责人", value: ""},
        {label: "执行人", value: ""}
    ])
    const columns_show: Ref = ref([
        {label: "ID(id)", value: true},
        {label: "申请人(base_name)", value: false},
        {label: "申请人部门(base_deptname)", value: false},
        {label: "单号(sericnum)", value: false},
        {label: "名称(name)", value: true},
        {label: "对应英文名(nameen)", value: true},
        {label: "所属单位(pid)", value: true},
        {label: "对应负责人(fuzename)", value: true},
        {label: "所在城市(city)", value: true},
        {label: "地址(address)", value: true},
        {label: "电话(tel)", value: true},
        {label: "传真(fax)", value: true},
        {label: "排序号(sort)", value: true},
        {label: "操作列", value: true},
    ]);
    const get_units = async (options = {}) => {
        const data: res = await get("/system/baseSet/unit", options);
        units.splice(0, units.length, ...data.data);
        page.value = data.page
        total.value = data.total
    };
    const unit_add = async () => {
        await post("/system/baseSet/unit/add", current_row);
        await get_units();
    };
    const on_search = async () => {
        await get_units({
            name: search_val.value
        })
    }
    const on_up_search = async () => {
        const options = {}
        search_obj.value.forEach(item => {
            Object.assign(options, {[item.key]: item.value})
        })
        await get_units(options)
    }
    const go = async () => {
        const to_page = Number(page.value) + 1
        if (to_page > total.value) {
            ElMessage.warning('已经是最后一页了')
            return
        }
        await get_units({page: to_page})
    }
    const back = async () => {
        const to_page = Number(page.value) - 1
        if (to_page < total.value) {
            ElMessage.warning('已经是第一页了')
            return
        }
        await get_units({page: to_page})
    }
    const to_head = async () => {
        await get_units({page: 1})
    }
    const to_end = async () => {
        await get_units({page: Number(total.value)})
    }
    const refresh = async () => {
        await get_units()
    }
    return {
        units,
        current_row,
        get_units,
        unit_add,
        columns_show,
        on_search,
        search_val,
        search_obj,
        search_obj_task,
        on_up_search,
        page,
        total,
        go,
        back,
        to_head,
        to_end,
        refresh
    };
});
